import cv2 as cv
import numpy as np
image = cv.imread(r'C:\Users\Admin\Desktop\imagedata\retif\7_11.png',0)
#  16:9

processed = cv.equalizeHist(image)
start = 0
end = (351, 471)
points = []
point = []
line = []
def var(l):
    length = len(l)
    avg = sum(l)/length
    s = 0
    for num in l:
        s += (num-avg)**2
    return s/length

# for x in range(start,end[0]+1):
#     for y in range(start, end[1]+1):
#         out = image[x:x+9,y:y+9]
#         print(out)
#         in_m = image[x+2:x+7,y+2:y+7]
#         #print(in_m)
#         avg_center = np.mean(in_m)
#         avg_around = (np.sum(out)-np.sum(in_m))/(81-25)
#         if avg_around-avg_center > 4:
#             points.append((x,y))
#             l = out.reshape(-1).tolist()
#             for i in range(20, 45):
#                 l.pop(i)
#             if var(l)>0:
#                 line.append((x,y))
l_kernels = [
            np.array([[10,3,0],
                      [3,0,-3],
                      [0,-3,-10]]),
            np.array([[0,3,10],
                      [-3,0,3],
                      [-10,-3,0]]),
            np.array([[3, 10, 3],
                      [0, 0, 0],
                      [-3, -10, -3]]),
            np.array([[3, 0, -3],
                      [10, 0, -10],
                      [3, 0, -3]]),
            np.array([[3,6,10,6,3],
                      [0,0,0,0,0],
                      [0,0,0,0,0],
                      [0,0,0,0,0],
                      [-3,-6,-10,-6,-3]]),

            np.array([[3,0,0,0,-3],
                      [6,0,0,0,-6],
                      [10,0,0,0,-10],
                      [6,0,0,0,-6],
                      [3,0,-3,0,-3]]),
            np.array([[10,6,3,0,0],
                      [6,0,0,0,0],
                      [3,0,0,0,-3],
                      [0,0,0,0,-6],
                      [0,0,-3,-6,-10]]),
            np.array([[0,0,-3,-6,-10],
                      [0,0,0,0,-6],
                      [3,0,0,0,-3],
                      [6,0,0,0,0],
                      [10,6,3,0,0]])
          ]
p_kernel = np.array([[9,9,9,9,9],
                      [9,-16,-16,-16,9],
                      [9,-16,-16,-16,9],
                      [9,-16,-16,-16,9],
                      [9,9,9,9,9]])
def conv(kernels, img, thresholds):
    kernel_sizes = [kernel.shape[0] for kernel in kernels]
    height, width = img.shape
    points = []
    for i in range(len(kernels)):

        for x in range(height-kernel_sizes[i]+1):
            for y in range(width-kernel_sizes[i]+1):
                if np.abs(np.sum(kernels[i] * img[x:x+kernel_sizes[i], y:y+kernel_sizes[i]]))>thresholds[i]:
                    points.append((x+(kernel_sizes[i]-1)//2,y+(kernel_sizes[i]-1)//2))
    return points

l_thresholds = [200,200,200,200,600,600,600,600]
l_set = conv(l_kernels, image, l_thresholds)
l_set = list(set(l_set))

pad_img = np.pad(image, 5, mode='edge')
p_threshold = 0
for x,y in l_set:
    around_x = np.hstack([pad_img[x+2:x+4,y+2:y+9], pad_img[x+7:x+9,y+2:y+9]])
    around_y = np.hstack([pad_img[x + 2:x + 9, y + 2:y + 4], pad_img[x + 2:x + 9, y + 7:y + 9]])
    if var(around_x.reshape(-1))+var(around_y.reshape(-1)) < p_threshold:
        l_set.remove((x,y))
# p_set = set(conv(p_kernel, image, p_threshold))
# points = l_set - p_set
def num_black(img, x, y, around=3):
    return np.sum(img[x-(around-1)//2:x+(around-1)//2,y-(around-1)//2:y+(around-1)//2]==0)
def calculateGradient(img, x, y):
    return (np.abs(img[x,y]-img[x+1,y])+np.abs(img[x,y]-img[x-1,y])+np.abs(img[x,y+1]-img[x,y])+np.abs(img[x,y]-img[x,y-1]))/4


# num_prec = len(l_set)
# num_cour = 0
# while num_prec-num_cour>50:
#     img_all_noted = image.copy()
#     for (x, y) in l_set:
#         img_all_noted[x, y] = 0
#     for (x,y) in l_set:
#         if num_black(img_all_noted, x, y, around=3)!=8:
#             l_set.remove((x,y))
#     num_prec = num_cour
#     num_cour = len(l_set)
# img_all_noted = image.copy()
# for (x,y) in l_set:
#     img_all_noted[x,y] = 0
# for (x,y) in l_set:
#     if num_black(img_all_noted, x, y,around=3)<=3:
#         l_set.remove((x,y))
for (x,y) in l_set:
    image[x,y] = 0

        # for i in range(9):
        #     for j in range(9):
        #         sum_3[i // 3 * 3 + j // 3] += image[x + i][y + j]
        #         sum_9 += image[x + i][y + j]
        # sum_9 //= (9 * 9)
        # min = sum_3[4]
        # flag = 0
        # c = 0
        # for num in range(9):
        #
        #
        #     if sum_3[num] >= min:
        #         if sum_3[num] - min >= 0:
        #             c += 1
        #             continue
        #
        # if c==9:
        #     flag = 1
        # if flag:
        #     print(c)
        #     points.append((x,y))
        #     sum_3.pop(4)
        #     variance = var(sum_3)
        #     print(variance)
        #     if variance > :
        #         line.append((x,y))
        #     else:
        #         point.append((x,y))

# for (x,y) in line:
#     image[x + 4][y + 4] = 0
    # image[x + 3][y + 4] = 0
    # image[x + 5][y + 4] = 0
    # image[x + 4][y + 3] = 0
    # image[x + 4][y + 5] = 0

'''
for (x, y) in point:
    image[x + 4][y + 4] = 0
    image[x + 3][y + 4] = 0
    image[x + 5][y + 4] = 0
    image[x + 4][y + 3] = 0
    image[x + 4][y + 5] = 0
'''
cv.imshow("all",image)
cv.imwrite(r'C:\Users\Admin\Desktop\reimage/0_0.png',image)
cv.waitKey(0)
print('')
